<title>明细</title>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
          <div class="LAY_wisdom_title">
            <h3 class="LAY_wisdom_title_line">提现周期</h3>
          </div>

          <form class="layui-form" lay-filter="LAY_revenue_detail_form">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">产品名称</label>
                <div class="layui-input-inline">
                  <input type="text" name="productName" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <label class="layui-form-label">投保人姓名</label>
                <div class="layui-input-inline">
                  <input type="text" name="holder" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
              </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">被保人姓名</label>
                <div class="layui-input-inline">
                  <input type="text" name="insured" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <label class="layui-form-label">保单号</label>
                <div class="layui-input-inline">
                  <input type="text" name="policyCode" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
              </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">入账日期</label>
                <div class="layui-input-inline">
                  <input type="text" name="time" id="LAY_range_time" placeholder="请选择" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <label class="layui-form-label">下单机构</label>
                <div class="layui-input-inline">
                  <select name="channel" lay-verify="" lay-search>
                    <option value="">请选择</option>
                  </select>
                </div>
              </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">推广费状态</label>
                <div class="layui-input-inline">
                  <select name="status" lay-verify="" lay-search>
                    <option value="">请选择</option>
                    <option value="1">已入账</option>
                    <option value="2">提现中</option>
                    <option value="3">已提现</option>
                    <option value="4">结算中</option>
                    <option value="5">退保平账</option>
                  </select>
                </div>
              </div>
            </div>
    
            <div class="layui-form-item">
              <button type="button" class="layui-btn LAY_revenue_back">
                <i class="layui-icon layui-icon-return layuiadmin-button-btn"></i>
              </button>
              <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY_revenue_detail_search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 查询
              </button>
              <button type="reset" class="layui-btn layuiadmin-btn-useradmin">
                <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 重置
              </button>
              <button type="button" class="layui-btn layuiadmin-btn-useradmin LAY_revenue_detail_export">
                <i class="layui-icon layui-icon-export layuiadmin-button-btn"></i> 导出
              </button>
            </div>
          </form>
        </div>

        <div class="layui-card-body">
          <div class="LAY_wisdom_title">
            <h3>搜索结果</h3>
          </div>

          <table id="LAY_revenue_detail" lay-filter="LAY_revenue_detail"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script>
  layui.use(['admin', 'form', 'laytpl', 'table', 'layer', 'laydate'], function () {
    var $ = layui.$,
      admin = layui.admin,
      setter = layui.setter,
      layer = layui.layer,
      form = layui.form,
      table = layui.table,
      laydate = layui.laydate,
      router = layui.router(),
      settlementCycle = router.search.settlementCycle;

    laydate.render({
      elem: '#LAY_range_time',
      range: true
    });

    admin.req({
      url: setter.url + '/org/currentAndChildrenLiteList',
      type: 'GET',
      done: function (res) {
        var data = res.data || [];

        layui.each(data, function (index, item) {
          var html = '<option value="' + item.orgCode + '">' + item.orgName + '</option>'
          $('select[name="channel"]').append(html);
        });

        // 初始化表单
        form.render(null, 'LAY_revenue_detail_form');

        // 表单提交
        form.on('submit(LAY_revenue_detail_search)', function (data) {
          var field = data.field;
          var params = $.extend(true, field, {
            settlementCycle: settlementCycle
          });
          params = generateParams(params);

          reloadDetailList(params, 1);
          return false;
        });
      }
    });

    initDetailList();
    initEvents();

    function initDetailList() {
      var field = form.val('LAY_revenue_detail_form');
      var params = $.extend(true, {
        settlementCycle: settlementCycle,
      } , field);

      table.render({
        elem: '#LAY_revenue_detail'
        ,where: params
        ,page: true
        ,url: setter.url + '/finance/withdrawalDetailList' //模拟接口
        ,parseData: function(res){
          return {
            "code": res.code,
            "msg": res.msg,
            "data": res.data.datas,
            "count": res.data.recordCounts,
          };
        }
        ,response: {
          statusCode: 200 //规定成功的状态码，默认：0
        } 
        ,request: {
          limitName: 'pageSize'
        }
        ,cols: [[
          { field: 'generateTime',align: 'center', title: '生成日期' }
          ,{ field: 'channel',align: 'center', title: '下单渠道' }
          ,{ field: 'policyCode',align: 'center', title: '保单号' }
          ,{ field: 'commType',align: 'center', title: '推广费类型' }
          ,{ field: 'createTime',align: 'center', title: '下单日期' }
          ,{ field: 'renewalTime',align: 'center', title: '续期应交日期' }
          ,{ field: 'realRenewalTime',align: 'center', title: '续期实交日期' }
          ,{ field: 'status',align: 'center', title: '推广费状态' }
          ,{ field: 'inAccountTime',align: 'center', title: '入账时间' }
          ,{ field: 'insPremium',align: 'center', title: '保费(元)' }
          ,{ field: 'commissionRate',align: 'center', title: '结算比例' }
          ,{ field: 'commission',align: 'center', title: '推广费(元)' }
          ,{ field: 'holder',align: 'center', title: '投保人姓名' }
          ,{ field: 'insured',align: 'center', title: '被保人姓名' }
        ]]
      });
    }

    function reloadDetailList(params, curr, callback) {
      table.reload('LAY_revenue_detail', {
        where: params
        ,page: {
          curr: curr
        }
        ,done: function (res) {
          if (typeof(callback) == 'function') {
            callback(res);
          }
        }
      })
    }

    function generateParams(params) {
      var result = $.extend(true, {}, params);
      var time = result.time.split(' - ') || [];

      result.startTime = time[0];
      result.endTime = time[1];
      delete result.time;

      return result;
    }

    function generateExportUrl(params) {
      var baseUrl = setter.url + '/finance/withdrawalDetailExport';
      var result = '';
      var keys = Object.keys(params);
      layui.each(keys, function (index, key) {
        if (params[key]) {
          result += '&' + key + '=' + params[key];
        }
      });

      if (!result) {
        return baseUrl;
      }

      result = result.replace(/&/, '?');
      return baseUrl + result;
    }

    function initEvents() {
      $('.LAY_revenue_detail_export').off('click').on('click', function () {
        var field = form.val('LAY_revenue_detail_form');
        var params = $.extend(true, {
          settlementCycle: settlementCycle,
        } , field);
        params = generateParams(params);
        var url = generateExportUrl(params);

        location.href = url;
      });

      $('.LAY_revenue_back').off('click').on('click', function () {
        admin.events.back();
      });
    }

  });
</script>